Public Class frmTheoDoiCongNoKhachHang_DakNong
    Dim sqlQuery As String = ""

    Private Sub btnXemThe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXemThe.Click

        Me.Cursor = Cursors.WaitCursor
        congno.tinhTonCongNo_KhachHang(Me.DTP_ThangNam.Value.Year, Me.DTP_ThangNam.Value.Month, 3)
        congno.raiNoKhachHang(Me.DTP_ThangNam.Value.Year, Me.DTP_ThangNam.Value.Month, Me.DTP_NgayTinhNo.Value)
        Me.Cursor = Cursors.Default

        Dim cycleID As String = Me.DTP_ThangNam.Value.Year & Format(Me.DTP_ThangNam.Value.Month, "00")

        sqlQuery = "DELETE FROM tbl_temp_theodoi_congno_khachhang"
        moduleDatabaseConn.ExecuteNonQuery(sqlQuery)

        sqlQuery = "INSERT INTO tbl_temp_theodoi_congno_khachhang"
        sqlQuery += " SELECT t1.ma_khachhang, t2.ten_nhacungcap, t2.diachi_nhacungcap"
        sqlQuery += ", t2.mucno, t1.thanhtien_cuoiky, t2.hanno, 0 , ''"
        sqlQuery += " FROM tbl_dot_toncongno_khachhang as t1, tbl_khachhang as t2"
        sqlQuery += " WHERE t1.ma_khachhang = t2.ma_nhacungcap"
        sqlQuery += " AND t1.recycleId = '" & cycleID & "'"
        moduleDatabaseConn.ExecuteNonQuery(sqlQuery)

        sqlQuery = "SELECT t1.ma_khachhang, SUM(t1.tienno_conlai)"
        sqlQuery += " FROM tbl_theodoi_no_khachhang as t1, tbl_khachhang as t2"
        sqlQuery += " WHERE t1.ma_khachhang = t2.ma_nhacungcap"
        sqlQuery += " AND DATEDIFF('" & Format(Me.DTP_NgayTinhNo.Value, "yyyy-MM-dd") & "', ngay_chungtu) >= t2.hanno"
        sqlQuery += " AND t1.loaiphieu = 5"
        sqlQuery += " GROUP BY t1.ma_khachhang"
        Dim data_temp As DataTable = moduleDatabaseConn.GetDataTable(sqlQuery)
        Dim ma_khachhang As String
        Dim noquahan As Double

        For i As Integer = 0 To data_temp.Rows.Count - 1
            ma_khachhang = data_temp.Rows.Item(i).Item(0)
            noquahan = data_temp.Rows.Item(i).Item(1)
            sqlQuery = "UPDATE tbl_temp_theodoi_congno_khachhang"
            sqlQuery += " SET noquahan = " & noquahan
            sqlQuery += " WHERE ma_khachhang = '" & ma_khachhang & "'"
            moduleDatabaseConn.ExecuteNonQuery(sqlQuery)
        Next

        sqlQuery = "SELECT * FROM tbl_temp_theodoi_congno_khachhang"
        Me.gridTheoDoiCongNo.DataSource = moduleDatabaseConn.GetDataTable(sqlQuery)
    End Sub

    Private Sub btnInBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInBaoCao.Click
        Dim myReport As New FrmCrTheoDoiCongNo_KhachHang_DakNong
        myReport.setThang(Format(Me.DTP_ThangNam.Value.Month, "00"))
        myReport.setNam(Me.DTP_ThangNam.Value.Year)
        myReport.Show()
    End Sub
    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub frmTheoDoiCongNoKhachHang_DakNong_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        moduleUtils.setDinhDangVietNam()
    End Sub
End Class